<?php

namespace App\Console\Commands;

use App\model\Area;
use App\model\Coustomer;
use App\model\Industry;
use App\model\Jinri;
use App\Repository\CustomerRepository;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class JinriNewCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'jinri';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * The console command description.
     *
     * @var string
     */

    protected  $client;
    protected   $customerRepository;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(Client $client,CustomerRepository $customerRepository)
    {
        $this->client=$client;
        parent::__construct();
        $this->customerRepository=$customerRepository;
    }


//头条获取数据接口

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $jiri=Jinri::where('is_display',0)->get();
   foreach ($jiri as $item){

        $config=$item;
        $host="https://feiyu.oceanengine.com/";
        $api="/crm/v2/openapi/pull-clues/";
        $date_s=Carbon::today()->toDateString();
        $date_e=Carbon::tomorrow()->toDateString();
        $now= time();
        $start_date = $date_s;
        $end_date= $date_e;
        $source_data = $api.'?start_time='.$start_date.'&end_time='.$end_date.' '.$now;
        $key=$config['key'];
        $tureOrFalse=true;
        $page=1;
        while ($tureOrFalse){
            $result= $this->client->get('https://feiyu.oceanengine.com/crm/v2/openapi/pull-clues/',[
                'headers'=>[
                    'signature'=>base64_encode(hash_hmac('sha256', $source_data, $key)),
                    'timestamp'=>$now,
                    'Access-Token'=>$config['token'],
                ],
                'query'=>[
                    'start_time'=>$start_date,
                    'end_time'=>$end_date,
                    'page'=>$page,
                    'page_size'=>100,
                ]
            ]);
            $page++;
            $app=json_decode( $result->getBody()->getContents(),true);

            if (!empty($app['data'])){

                foreach ($app['data'] as $v){
                    /*$coustomer=Coustomer::whereMobile($v['telphone'])
                        ->where('project_id',$config['project_id'])
                        ->where('created_at','>',Carbon::today())
                        ->first();*/

                    $coustomer=Coustomer::where('clue_id','=',$v['clue_id'])
                        ->first();

                    if (!$coustomer){
                        $coustomer=new Coustomer();
                        $coustomer->name=$v['name'];
                        $coustomer->mobile=$v['telphone'];
                        if (isset($v['location'])){
                            $city=explode('+',$v['location']);
                            $coustomer->city1=$city[0];
                            $coustomer->city2=$city[1];
                        }
                        $coustomer->source_id=9;
                        $coustomer->project_id=$config['project_id'];
                        if (isset($v['remark_dict']['所在行业'])){
                            $industry=Industry::firstOrCreate(['name'=>$v['remark_dict']['所在行业'],'project_id'=>$config['project_id']]);
                            $coustomer->industry_id=$industry->industry_id;

                        }

                        $coustomer->clue_id=$v['clue_id'];
                        if (isset($v['remark_dict']['需求面积'])){
                            $area=Area::firstOrCreate(['name'=>$v['remark_dict']['需求面积']]);
                            $coustomer->area_id=$area->id;
                        }
                        if (isset($v['remark_dict']['需求的面积'])){
                            $area=Area::firstOrCreate(['name'=>$v['remark_dict']['需求的面积']]);
                            $coustomer->area_id=$area->id;
                        }
                        if (isset($v['remark_dict']['所需面积'])){
                            $area=Area::firstOrCreate(['name'=>$v['remark_dict']['所需面积']]);
                            $coustomer->area_id=$area->id;
                        }
                        if (isset($v['remark_dict']['所需求的面积'])){
                            $area=Area::firstOrCreate(['name'=>$v['remark_dict']['所需求的面积']]);
                            $coustomer->area_id=$area->id;
                        }
                        if (isset($v['remark_dict']['所需厂房面积'])){
                            $area=Area::firstOrCreate(['name'=>$v['remark_dict']['所需厂房面积']]);
                            $coustomer->area_id=$area->id;
                        }
                        $coustomer->adv_name=$v['ad_plan_name'];
                        $coustomer->external_url=$v['external_url'];
                        $coustomer->sex=$v['gender'];
                        $coustomer->save();
                        $this->info($coustomer->customer_id);
                        //销售数据
                        $this->customerRepository->sendXiaoshou($coustomer);
                    }

                }
            }else{
                $tureOrFalse=false;
            }
        }
    }
    }
}
